package com.applications.addressbookapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AddressApp extends ListActivity {

	Intent intent;
	TextView contactId;
	 
    // The object that allows me to manipulate the database 
    DBTools dbTools = new DBTools(this);
 
    // Called when the Activity is first called
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_app);
		ArrayList<HashMap<String, String>> contactList =  dbTools.getAllContacts();
		
		if(contactList.size()!=0) {
				             
        // Get the ListView and assign an event handler to it
         
        ListView listView = getListView();
        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				contactId = (TextView) view.findViewById(R.id.contactId);
				String contactIdValue = contactId.getText().toString();
				 Intent  theIndent = new Intent(getApplication(),EditContact.class);
                 theIndent.putExtra("contactId", contactIdValue);
                 startActivity(theIndent);
			}             
        });	
		
        ListAdapter adapter = new SimpleAdapter(AddressApp.this, contactList, R.layout.contact_entry, 
        					new String[]{"contactId", "lastName","firstName","phoneNumber"},
        					new int[]{R.id.contactId,R.id.lastName1,R.id.firstName1,R.id.phoneNumber});
        
        setListAdapter(adapter);
	
		}
	}
	
	 public void showAddContact(View view) {
         Intent theIntent = new Intent(getApplication(), NewContact.class);
         startActivity(theIntent);
     }
	 
	 public void searchContact(View view) {
         Intent theIntent = new Intent(getApplication(), SearchContact.class);
         startActivity(theIntent);
     }
 

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.address_app, menu);
		return true;
	}

}
